﻿#include <graphics.h>	
#include <conio.h>
#include <easyx.h>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef struct Obstacle {
	int x, y;
	int width, height;
}obs;
int xHP;
int yHP;
int timeCounter = 0;  // 时间计数器，单位为毫秒
bool visible = false;

bool rl = true;
bool up = false;
bool down = false;
bool rl2 = false;
bool up2 = false;
bool down2 = false;
bool battack = false;
bool battack2 = false;
bool bskill = false;
bool bskill2 = false;
bool p1win = false;
bool p2win = false;

ExMessage msgm;
IMAGE start;
IMAGE map1;
IMAGE rplayerone[3];
IMAGE rplayeronem[3];
IMAGE lplayerone[3];
IMAGE lplayeronem[3];
IMAGE attack;
IMAGE attackm;
IMAGE rskill;
IMAGE lskill;
IMAGE rskill2;
IMAGE lskill2;
IMAGE attack2;
IMAGE lplayertwo[3];
IMAGE rplayertwo[3];
IMAGE HealthPotion;
IMAGE endp1;
IMAGE endp2;
IMAGE gamesm;

int gamecount = 0;
int indexplayerone = 0;
int frameDelay = 15;
int frameTime = 0;
int frameStart = 0;
//行走帧率
const int backframeDelay = 1;
int backframeTime = 0;
int backframeStart = 0;
//后退帧率
int backspeed = 10;
//后退速度
int attacksize = 60;
int playersize = 60;
int skillh = 60;
int skillw = 360;

int fushHP = 5000;
//攻击时间
int attacktime;
int attacktime2;
int skilltime;
int skilltime2;

int startattacktime = 0;
int endattacktime = 0;
int startattacktime2 = 0;
int endattacktime2 = 0;
int attackspeed = 1000;

int startskilltime = 0;
int endskilltime = 0;
int startskilltime2 = 0;
int endskilltime2 = 0;
int skillspeed = 1000;

int timenewMP = 5;

int attacknewMP = 10;
int attacknewMP2 = 10;

int jumph = 200;
int jumpspeed = 10;
int playerspeed;
int playerspeed2;

// 定义初始血量和蓝量
int currentHP = 100;
int currentMP = 100;
int currentHP2 = 100;
int currentMP2 = 100;

// 定义血条和蓝条的位置和尺寸
const int HP_BAR_X = 0;
const int HP_BAR_Y = 0;
const int HP_BAR_WIDTH = 200;
const int HP_BAR_HEIGHT = 30;
const int MP_BAR_X = 0;
const int MP_BAR_Y = 50;

const int MP_BAR_WIDTH = 200;
const int MP_BAR_HEIGHT = 30;

const int HP_BAR_X2 = 1024 - HP_BAR_WIDTH - 4;
const int HP_BAR_Y2 = 0;
const int MP_BAR_X2 = 1024 - MP_BAR_WIDTH - 4;
const int MP_BAR_Y2 = 50;

int startMPtime = 0;
int newMPtime;
int startMPtime2 = 0;
int newMPtime2;

int nowy;
int nowy2;
int temp;
int temp2;

obs yellowbox;
obs house;
obs longgun;
obs shortgun;
//设置障碍物
void initimage();//加载图片
void startgraph();//开始界面
bool startbutton();//开始的按钮的实现与判断点击
bool inArea();//判断点击位置是否在按钮处
void initstarttext();//设置开始界面的字体的位置和类别并将他们变为按钮
//peekmessage(&msgm,EX_MOUSE)获取鼠标信息
bool pdobs(int* x, int* y);
bool pdobs2(int* x, int* y);
void show(int x, int y, int x2, int y2);
void game();

void initimage()
{
	char path_rplayerone[100];
	char path_lplayerone[100];
	char path_rplayertwo[100];
	char path_lplayertwo[100];
	char path_lplayeronem[100];
	char path_rplayeronem[100];
	loadimage(&start, "图片/start1.jpg", 1024, 600);
	loadimage(&map1, "图片/start72.jpg", 1024, 600);
	loadimage(&attack, "图片/attack.jpg", playersize, playersize);
	//loadimage(&attackm, "图片/attackm.jpg", 60, 60);
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_rplayerone, "图片/rplayerone%d.jpg", i + 1);
		loadimage(rplayerone + i, path_rplayerone, playersize, playersize);
	}
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_rplayeronem, "图片/rplayeronem%d.jpg", i + 1);
		loadimage(rplayeronem + i, path_rplayeronem, playersize, playersize);
	}
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_lplayerone, "图片/lplayerone%d.jpg", i + 1);
		loadimage(lplayerone + i, path_lplayerone, playersize, playersize);
	}
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_lplayeronem, "图片/lplayeronem%d.jpg", i + 1);
		loadimage(lplayeronem + i, path_lplayeronem, playersize, playersize);
	}
	loadimage(&rskill, "图片/rskill.jpg", 360, playersize);
	loadimage(&lskill, "图片/lskill.jpg", 360, playersize);
	loadimage(&rskill2, "图片/rskill2.jpg", 360, playersize);
	loadimage(&lskill2, "图片/lskill2.jpg", 360, playersize);
	loadimage(&attack2, "图片/attack2.jpg", playersize, playersize);
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_rplayertwo, "图片/rplayertwo%d.jpg", i + 1);
		loadimage(rplayertwo + i, path_rplayertwo, playersize, playersize);
	}
	for (int i = 0; i < 3; i++) {
		sprintf_s(path_lplayertwo, "图片/lplayertwo%d.jpg", i + 1);
		loadimage(lplayertwo + i, path_lplayertwo, playersize, playersize);
	}
	loadimage(&HealthPotion, "图片/Healthpotion.jpg", 30, 30);
	loadimage(&endp1, "图片/endp1.jpg", 300, 300);
	loadimage(&endp2, "图片/endp2.jpg", 300, 300);
	loadimage(&gamesm, "图片/gamesm.jpg", 1024, 600);
}
//加载图片
void randomize() {
	xHP = rand() % (1024 - 30);
	if (xHP <= yellowbox.x + yellowbox.width ) {
		xHP = yellowbox.x + yellowbox.width;
	}
	yHP = longgun.y-30;
	visible = true;
}
//随机生成药
bool inArea(int mx, int my, int x, int y, int w, int h)
{
	if (mx > x && mx<x + w && my>y && my < y + h)
	{
		return true;
	}
	else {
		return false;
	}
}
//判断鼠标是否在特点区域
bool startbutton(int x, int y, int w, int h, const char* text)
{
	BeginBatchDraw();
	if (inArea(msgm.x, msgm.y, x - 20, y - 20, w + 40, h + 40))
	{
		setfillcolor(RGB(93, 107, 153));
	}
	else {
		setfillcolor(RGB(229, 231, 232));//改变填充颜色
	}//按钮的变色
	fillroundrect(x - 20, y - 20, x + w + 20, y + h + 20, 20, 20);//边框
	settextcolor(BLACK);//设置字体颜色
	outtextxy(x, y, text);//显示字体
	EndBatchDraw();
	if (msgm.message == WM_LBUTTONDOWN && inArea(msgm.x, msgm.y, x - 20, y - 20, w + 40, h + 40))
	{
		return true;
	}
	return false;
}
//设置按钮并判断按下
bool judges()
{
	settextstyle(50, 20, "‌黑体");//设置字体
	char ks[] = "开始游戏";
	char ed[] = "结束游戏";
	int kw = 1024 / 2 - textwidth(ks) / 2;
	int kh = 600 / 2 - textheight(ks) / 2 + 70;
	int ew = 1024 / 2 - textwidth(ed) / 2;
	int eh = 600 / 2 - textheight(ed) / 2 + 180;//让框居中
	while (1)
	{
		peekmessage(&msgm, EX_MOUSE);
		if (startbutton(kw, kh, textwidth(ks), textheight(ks), ks)) {
			return true;
		}
		if (startbutton(ew, eh, textwidth(ed), textheight(ed), ed)) {
			return false;
		}
	}
}
//判断游戏开始
void selectMaps() {
	cleardevice();
	setbkmode(TRANSPARENT);
	putimage(0, 0, &map1);
}
//绘制血条
void drawHPBar1() {
	setfillcolor(RED);
	fillrectangle(HP_BAR_X, HP_BAR_Y, HP_BAR_X + currentHP * HP_BAR_WIDTH / 100, HP_BAR_Y + HP_BAR_HEIGHT);
	setcolor(WHITE);
	rectangle(HP_BAR_X, HP_BAR_Y, HP_BAR_X + HP_BAR_WIDTH, HP_BAR_Y + HP_BAR_HEIGHT);
}
//绘制蓝条
void drawMPBar1() {
	setfillcolor(BLUE);
	fillrectangle(MP_BAR_X, MP_BAR_Y, MP_BAR_X + currentMP * MP_BAR_WIDTH / 100, MP_BAR_Y + MP_BAR_HEIGHT);
	setcolor(WHITE);
	rectangle(MP_BAR_X, MP_BAR_Y, MP_BAR_X + MP_BAR_WIDTH, MP_BAR_Y + MP_BAR_HEIGHT);
}
//绘制血条
void drawHPBar2() {
	setfillcolor(RED);
	fillrectangle(HP_BAR_X2, HP_BAR_Y2, HP_BAR_X2 + currentHP2 * HP_BAR_WIDTH / 100, HP_BAR_Y2 + HP_BAR_HEIGHT);
	setcolor(WHITE);
	rectangle(HP_BAR_X2, HP_BAR_Y2, HP_BAR_X2 + HP_BAR_WIDTH, HP_BAR_Y2 + HP_BAR_HEIGHT);
}
//绘制蓝条
void drawMPBar2() {
	setfillcolor(BLUE);
	fillrectangle(MP_BAR_X2, MP_BAR_Y2, MP_BAR_X2 + currentMP2 * MP_BAR_WIDTH / 100, MP_BAR_Y2 + MP_BAR_HEIGHT);
	setcolor(WHITE);
	rectangle(MP_BAR_X2, MP_BAR_Y2, MP_BAR_X2 + MP_BAR_WIDTH, MP_BAR_Y2 + MP_BAR_HEIGHT);
}

void setobs(void) {
	yellowbox.height = 40;
	yellowbox.width = 81;
	yellowbox.x = 273;
	yellowbox.y = 411;
	house.x = 115;
	house.y = 453;
	house.height = 145;
	house.width = 909;
	shortgun.width = 113;
	shortgun.height = 10;
	shortgun.x = 2;
	shortgun.y = 531;
	longgun.height = 12;
	longgun.width = 721;
	longgun.x = 273;
	longgun.y = 313;
	return;
}
//制作障碍物
bool pdobs(int* x, int* y) {
	if (*x + playersize >= house.x - 2 && *y + playersize > house.y && rl) {
		return false;
	}
	if (*x + playersize >= yellowbox.x && *x < yellowbox.x + yellowbox.width - 10 && *y + playersize >= yellowbox.y + 10 && rl) {
		return false;
	}
	if (*x <= yellowbox.x + yellowbox.width && *x + playersize > yellowbox.x + 10 && *y + playersize >= yellowbox.y + 10 && !rl) {
		return false;
	}
	if (*x <= 0 && !rl) {
		return false;
	}
	if (*x + playersize >= 1024 && rl) {
		return false;
	}
	if (*x + playersize >= yellowbox.x + yellowbox.width + 2 && *y + playersize > longgun.y + 10 && rl) {
		return false;
	}
	return true;
}
bool pdobs2(int* x, int* y) {
	if (*x + playersize >= house.x - 2 && *y + playersize > house.y && rl2) {
		return false;
	}
	if (*x + playersize >= yellowbox.x && *x < yellowbox.x + yellowbox.width - 10 && *y + playersize >= yellowbox.y + 10 && rl2) {
		return false;
	}
	if (*x <= yellowbox.x + yellowbox.width && *x + playersize > yellowbox.x + 10 && *y + playersize >= yellowbox.y + 10 && !rl2) {
		return false;
	}
	if (*x <= 0 && !rl2) {
		return false;
	}
	if (*x + playersize >= 1024 && rl2) {
		return false;
	}
	if (*x + playersize >= yellowbox.x + yellowbox.width + 2 && *y + playersize > longgun.y + 10 && rl2) {
		return false;
	}
	return true;
}
//判断障碍物
bool pdmovedown(int* x, int* y) {
	if (*x <= house.x - playersize && *y + playersize <= house.y) {
		down = true;
		return true;
	}
	else if (*x + playersize < yellowbox.x && *y + playersize < house.y) {
		down = true;
		return true;
	}
	else if (*x + playersize <= yellowbox.x + yellowbox.width && *y + playersize < yellowbox.y) {
		down = true;
		return true;
	}
	return false;
}
bool pdmovedown2(int* x, int* y) {
	if (*x <= house.x - playersize && *y + playersize <= house.y) {
		down2 = true;
		return true;
	}
	else if (*x + playersize < yellowbox.x && *y + playersize < house.y) {
		down2 = true;
		return true;
	}
	else if (*x + playersize <= yellowbox.x + yellowbox.width && *y + playersize < yellowbox.y) {
		down2 = true;
		return true;
	}
	return false;
}
//判断移动的位置是否下降，并进行下降
void show(int x, int y, int x2, int y2) {
	BeginBatchDraw();
	putimage(0, 0, &map1);
	drawHPBar2();
	drawMPBar2();
	drawHPBar1();
	drawMPBar1();
	if (visible) {
		putimage(xHP, yHP, &HealthPotion, SRCPAINT);
	}
	if (rl) {
		putimage(x, y, &rplayerone[0], SRCPAINT);
		if (battack) {
			putimage(x + playersize, y, &attack, SRCPAINT);
		}
		if (bskill) {
			putimage(x + playersize, y, &rskill, SRCPAINT);
		}
	}
	else {
		putimage(x, y, &lplayerone[0], SRCPAINT);
		if (battack) {
			putimage(x - playersize, y, &attack, SRCPAINT);
		}
		if (bskill) {
			putimage(x - skillw, y, &lskill, SRCPAINT);
		}
	}
	if (rl2) {
		putimage(x2, y2, &rplayertwo[0], SRCPAINT);
		if (battack2) {
			putimage(x2 + playersize, y2, &attack2, SRCPAINT);
		}
		if (bskill2) {
			putimage(x2 + playersize, y2, &rskill2, SRCPAINT);
		}
	}
	else {
		putimage(x2, y2, &lplayertwo[0], SRCPAINT);
		if (battack2) {
			putimage(x2 - playersize, y2, &attack2, SRCPAINT);
		}
		if (bskill2) {
			putimage(x2 - skillw, y2, &lskill2, SRCPAINT);
		}
	}
	EndBatchDraw();
	return;
}
//显示p1和p2的具体位置
void pdHP(int x,int y,int p) {
	if ((x + playersize > xHP && x < xHP + 30) && (y < yHP + 30 && y + playersize > yHP)) {
		if (p == 2) {
			if (currentHP2 <= 100) {
				currentHP2 += 30;
			}
			if (currentHP2 >= 100) {
				currentHP2 = 100;
			}
		}
		else if (p == 1) {
			if (currentHP <= 100) {
				currentHP += 30;
			}
			if (currentHP >= 100) {
				currentHP = 100;
			}
		}
		visible = false;
		xHP = -100;
		yHP = -100;
	}
	return;
}
//判断是否捡到药瓶并进行消失
void pdskillandattacktime() {
	if (battack) {
		if (clock() - attacktime >= 500) {
			battack = false;
		}
	}
	if (battack2) {
		if (clock() - attacktime2 >= 500) {
			battack2 = false;
		}
	}
	if (bskill) {
		if (clock() - skilltime >= 1000) {
			bskill = false;
		}
	}
	if (bskill2) {
		if (clock() - skilltime2 >= 1000) {
			bskill2 = false;
		}
	}

	return;
}
//攻击持续进行的判断
void rback(int* x, int* y, int* x2, int* y2, int newx, int p) {
	if (p == 1) {
		rl = true;
		while (*x <= newx) {
			if (pdobs(x, y)) {
				*x += backspeed;
			}
			else {
				return;
			}
			backframeStart = clock();
			show(*x, *y, *x2, *y2);
			backframeTime = clock() - backframeStart;
			if (backframeTime <= backframeDelay) {
				Sleep(backframeDelay - backframeTime);
			}
		}
	}
	else if (p == 2) {
		rl2 = true;
		while (*x2 <= newx) {
			if (pdobs2(x2, y2)) {
				*x2 += backspeed;
			}
			else {
				return;
			}
			backframeStart = clock();
			show(*x, *y, *x2, *y2);
			backframeTime = clock() - backframeStart;
			if (backframeTime <= backframeDelay) {
				Sleep(backframeDelay - backframeTime);
			}
		}
	}
	return;
}
void lback(int* x, int* y, int* x2, int* y2, int newx, int p) {
	if (p == 1) {
		rl = false;
		while (*x + playersize >= newx) {
			if (pdobs(x, y)) {
				*x -= backspeed;
			}
			else {
				return;
			}
			backframeStart = clock();
			show(*x, *y, *x2, *y2);
			backframeTime = clock() - backframeStart;
			if (backframeTime <= backframeDelay) {
				Sleep(backframeDelay - backframeTime);
			}
		}
	}
	else if (p == 2) {
		rl2 = false;
		while (*x2 + playersize >= newx) {
			if (pdobs2(x2, y2)) {
				*x2 -= backspeed;
			}
			else {
				return;
			}
			backframeStart = clock();
			show(*x, *y, *x2, *y2);
			backframeTime = clock() - backframeStart;
			if (backframeTime <= backframeDelay) {
				Sleep(backframeDelay - backframeTime);
			}
		}
	}
	return;
}
//进攻实现后退
bool pdskillandattackheart(int* x, int* y, int* x2, int* y2) {
	if (battack) {
		if (rl) {
			if ((*x2 > *x && *x2 <= *x + playersize + attacksize) && (*y2 + playersize > *y && *y2 < *y + playersize)) {
				if (currentHP2 > 0) {
					currentHP2 -= 10;
				}
				if (currentHP2 <= 0) {
					currentHP2 = 0;
				}
				if (currentMP <= 100) {
					currentMP += attacknewMP;
				}
				if (currentMP >= 100) {
					currentMP = 100;
				}
				rback(x, y, x2, y2, *x + attacksize + playersize, 2);
				battack = false;
				return true;
			}
		}
		else {
			if ((*x2 >= *x - playersize - attacksize && *x2 < *x) && (*y2 + playersize > *y && *y2 < *y + playersize)) {
				if (currentHP2 > 0) {
					currentHP2 -= 10;
				}
				if (currentHP2 <= 0) {
					currentHP2 = 0;
				}
				if (currentMP <= 100) {
					currentMP += attacknewMP;
				}
				if (currentMP >= 100) {
					currentMP = 100;
				}
				lback(x, y, x2, y2, *x - attacksize, 2);
				battack = false;
				return true;
			}
		}
	}
	if (bskill) {
		if (rl) {
			if ((*x2 > *x && *x2 <= *x + playersize + skillw) && (*y2 + playersize > *y && *y2 < *y + playersize)) {
				if (currentHP2 > 0) {
					currentHP2 -= 25;
				}
				if (currentHP2 <= 0) {
					currentHP2 = 0;
				}
				rback(x, y, x2, y2, *x + skillw + playersize, 2);
				bskill = false;
				return true;
			}
		}
		else {
			if ((*x2 >= *x - playersize - skillw && *x2 < *x) && (*y2 + playersize > *y && *y2 < *y + playersize)) {
				if (currentHP2 > 0) {
					currentHP2 -= 25;
				}
				if (currentHP2 <= 0) {
					currentHP2 = 0;
				}
				lback(x, y, x2, y2, *x - skillw, 2);
				bskill = false;
				return true;
			}
		}
	}
	if (battack2) {
		if (rl2) {
			if ((*x > *x2 && *x <= *x2 + playersize + attacksize) && (*y + playersize > *y2 && *y < *y2 + playersize)) {
				if (currentHP > 0) {
					currentHP -= 10;
				}
				if (currentHP <= 0) {
					currentHP = 0;
				}
				if (currentMP2 <= 100) {
					currentMP2 += attacknewMP;
				}
				if (currentMP2 >= 100) {
					currentMP2 = 100;
				}
				rback(x, y, x2, y2, *x2 + attacksize + playersize, 1);
				battack2 = false;
				return true;
			}
		}
		else {
			if ((*x >= *x2 - playersize - attacksize && *x < *x2) && (*y + playersize > *y2 && *y < *y2 + playersize)) {
				if (currentHP > 0) {
					currentHP -= 10;
				}
				if (currentHP <= 0) {
					currentHP = 0;
				}
				if (currentMP2 <= 100) {
					currentMP2 += attacknewMP;
				}
				if (currentMP2 >= 100) {
					currentMP2 = 100;
				}
				lback(x, y, x2, y2, *x2 - attacksize, 1);
				battack2 = false;
				return true;
			}
		}

	}
	if (bskill2) {
		if (rl2) {
			if ((*x > *x2 && *x <= *x2 + playersize + skillw) && (*y + playersize > *y2 && *y < *y2 + playersize)) {
				if (currentHP > 0) {
					currentHP -= 25;
				}
				if (currentHP <= 0) {
					currentHP = 0;
				}
				rback(x, y, x2, y2, *x2 + skillw + playersize, 1);
				bskill2 = false;
				return true;
			}
		}
		else {
			if ((*x >= *x2 - playersize - skillw && *x < *x2) && (*y + playersize > *y2 && *y < *y2 + playersize)) {
				if (currentHP > 0) {
					currentHP -= 25;
				}
				if (currentHP <= 0) {
					currentHP = 0;
				}
				lback(x, y, x2, y2, *x2 - skillw, 1);
				bskill2 = false;
				return true;
			}
		}
	}
	return false;
}
//判断攻击成功，并进行击退
void playermove(int* x, int* y, int* x2, int* y2) {
	pdskillandattacktime();
	if (pdskillandattackheart(x, y, x2, y2)) {
		show(*x, *y, *x2, *y2);
		pdmovedown(x, y);
		pdmovedown2(x2, y2);
		return;
	}
	//向右移动
	if (GetAsyncKeyState('D') & 0x8000) {
		if (!bskill && !battack) {
			bool END = false;
			rl = true;
			while (indexplayerone < 4) {
				if (indexplayerone >= 3) {
					indexplayerone -= 3;
					END = true;
				}
				show(*x, *y, *x2, *y2);
				if (pdobs(x, y)) {
					*x = *x + playerspeed;
				}
				pdmovedown(x, y);
				indexplayerone++;
				if (!GetAsyncKeyState('D')) {
					break;
				}//防止期间按相反的键继续向前行走
				if (END) {
					break;
				}
			}
			indexplayerone = 0;
		}
	}
	//向左移动
	if (GetAsyncKeyState('A') & 0x8000) {
		if (!bskill && !battack) {
			bool END = false;
			rl = false;
			while (indexplayerone < 4) {
				if (indexplayerone >= 3) {
					indexplayerone -= 3;
					END = true;
				}
				show(*x, *y, *x2, *y2);
				if (pdobs(x, y)) {
					*x = *x - playerspeed;
				}
				pdmovedown(x, y);
				indexplayerone++;

				if (!GetAsyncKeyState('A')) {
					break;
				}//防止期间按相反的键继续向前行走

				if (END) {
					break;
				}
			}
			indexplayerone = 0;
		}
	}
	//普通攻击
	if (GetAsyncKeyState('J') & 0x8000) {
		startattacktime = clock();
		if (startattacktime - endattacktime >= 2 * attackspeed || endattacktime <= 0) {
			battack = true;
			attacktime = clock();
			if (rl) {
				//putimage(x + 60, y, &attackm, NOTSRCERASE);
				BeginBatchDraw();
				putimage(*x + playersize, *y, &attack, SRCPAINT);
				EndBatchDraw();
				cleardevice();
				show(*x, *y, *x2, *y2);
			}
			else {
				//putimage(x - 60, y, &attackm, NOTSRCERASE);
				BeginBatchDraw();
				putimage(*x - attacksize, *y, &attack, SRCPAINT);
				EndBatchDraw();
				cleardevice();
				show(*x, *y, *x2, *y2);
			}
			endattacktime = clock();
		}
	}
	//放技能
	if (GetAsyncKeyState('K') & 0x8000) {
		startskilltime = clock();
		if (startskilltime - endskilltime >=  skillspeed || endskilltime <= 0) {
			if (currentMP >= 50) {
				currentMP = currentMP - 50;
				bskill = true;
				skilltime = clock();
				if (rl) {
					BeginBatchDraw();
					putimage(*x + playersize, *y, &rskill, SRCPAINT);
					EndBatchDraw();
					cleardevice();
					show(*x, *y, *x2, *y2);
				}
				else {
					BeginBatchDraw();
					putimage(*x - skillw, *y, &lskill, SRCPAINT);
					EndBatchDraw();
					cleardevice();
					show(*x, *y, *x2, *y2);
				}
				endskilltime = clock();
			}
		}
	}
	//跳跃
	if (GetAsyncKeyState('W') & 0x8000) {
		if (!bskill && !battack) {
			if (!up && !down) {
				nowy = *y;
				temp = *y;
				up = true;
				down = true;
			}
		}
	}
	//向右移动
	if (GetAsyncKeyState(VK_RIGHT) & 0x8000) {
		if (!bskill2 && !battack2) {
			bool END = false;
			rl2 = true;
			while (indexplayerone < 4) {
				if (indexplayerone >= 3) {
					indexplayerone -= 3;
					END = true;
				}
				show(*x, *y, *x2, *y2);
				if (pdobs2(x2, y2)) {
					*x2 = *x2 + playerspeed2;
				}
				pdmovedown2(x2, y2);
				indexplayerone++;
				if (!GetAsyncKeyState(VK_RIGHT)) {
					break;
				}//防止期间按相反的键继续向前行走
				if (END) {
					break;
				}
			}
			indexplayerone = 0;
		}
	}
	//向左移动
	if (GetAsyncKeyState(VK_LEFT) & 0x8000) {
		if (!bskill2 && !battack2) {
			bool END = false;
			rl2 = false;
			while (indexplayerone < 4) {
				if (indexplayerone >= 3) {
					indexplayerone -= 3;
					END = true;
				}
				show(*x, *y, *x2, *y2);
				if (pdobs2(x2, y2)) {
					*x2 = *x2 - playerspeed2;
				}
				pdmovedown2(x2, y2);
				indexplayerone++;
				if (!GetAsyncKeyState(VK_LEFT)) {
					break;
				}//防止期间按相反的键继续向前行走

				if (END) {
					break;
				}
			}
			indexplayerone = 0;
		}
	}
	//普通攻击
	if (GetAsyncKeyState(VK_NUMPAD1) & 0x8000) {
		startattacktime2 = clock();
		if (startattacktime2 - endattacktime2 >= 2 * attackspeed || endattacktime2 <= 0) {
			battack2 = true;
			attacktime2 = clock();
			if (rl2) {
				//putimage(x + 60, y, &attackm, NOTSRCERASE);
				BeginBatchDraw();
				putimage(*x2 + playersize, *y2, &attack2, SRCPAINT);
				EndBatchDraw();
				cleardevice();
				show(*x, *y, *x2, *y2);
			}
			else {
				//putimage(x - 60, y, &attackm, NOTSRCERASE);
				BeginBatchDraw();
				putimage(*x2 - attacksize, *y2, &attack2, SRCPAINT);
				EndBatchDraw();
				cleardevice();
				show(*x, *y, *x2, *y2);
			}
			endattacktime2 = clock();
		}
	}
	//放技能
	if (GetAsyncKeyState(VK_NUMPAD2) & 0x8000) {
		startskilltime2 = clock();
		if (startskilltime2 - endskilltime2 >= skillspeed || endskilltime2 <= 0) {
			if (currentMP2 >= 50) {
				currentMP2 = currentMP2 - 50;
				bskill2 = true;
				skilltime2 = clock();
				if (rl2) {
					BeginBatchDraw();
					putimage(*x2 + playersize, *y2, &rskill2, SRCPAINT);
					EndBatchDraw();
					cleardevice();
					show(*x, *y, *x2, *y2);
				}
				else {
					BeginBatchDraw();
					putimage(*x2 - skillw, *y2, &lskill2, SRCPAINT);
					EndBatchDraw();
					cleardevice();
					show(*x, *y, *x2, *y2);
				}
				endskilltime2 = clock();
			}
		}
	}
	//跳跃
	if (GetAsyncKeyState(VK_UP) & 0x8000) {
		if (!bskill2 && !battack2) {
			if (!up2 && !down2) {
				nowy2 = *y2;
				temp2 = *y2;
				up2 = true;
				down2 = true;
			}
		}
	}
	//暂停
	if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
		cleardevice();
		setbkcolor(BLACK);
		settextstyle(50, 20, "黑体");
		char re[] = "返回主菜单";
		char co[] = "继续游戏";
		int rw = 1024 / 2 - textwidth(re) / 2;
		int rh = 600 / 2 - textheight(re) / 2 - 80;
		int cw = 1024 / 2 - textwidth(co) / 2;
		int ch = 600 / 2 - textheight(co) / 2 + 80;
		while (1) {
			peekmessage(&msgm, EX_MOUSE);
			if (startbutton(rw, rh, textwidth(re), textheight(re), re)) {
				game();
			}
			if (startbutton(cw, ch, textwidth(co), textheight(co), co)) {
				return;
			}
		}
	}
	return;
}
//游戏的按键实现
bool pdwin() {
	if (currentHP == 0 || currentHP2 == 0) {
		return true;
	}
	return false;
}
//判断输赢
void playertrue(int x, int y, int x2, int y2) {
	while (1) {
		pdHP(x, y, 1);
		pdHP(x2, y2, 2);
		timeCounter += 10;  // 每次循环增加 10 毫秒(回复药水出现CD)
		if (timeCounter >= 5000 && visible == false) {
			randomize();
			timeCounter = 0;  // 重置计数器
		}
		frameStart = clock();
		if (up) {
			if (x >= house.x - playersize && x <= 1024) {
				if (x + playersize >= yellowbox.x && x + playersize <= yellowbox.x + yellowbox.width) {
					nowy = yellowbox.y - playersize;
				}
				else if (x + playersize > yellowbox.x + yellowbox.width) {
					nowy = longgun.y - playersize;
				}
				else {
					nowy = house.y - playersize;
				}
			}
			else if (x + playersize < house.x) {
				nowy = shortgun.y - playersize;
			}
			else {
				nowy = temp;
			}
			y -= jumpspeed;
			show(x, y, x2, y2);
			if (y <= temp - jumph) {
				up = false;
			}
		}
		if (down && !up) {
			if (x > house.x - playersize && x <= 1024) {
				if (x + playersize >= yellowbox.x && x + playersize <= yellowbox.x + yellowbox.width) {
					nowy = yellowbox.y - playersize;
				}
				else if (x + playersize > yellowbox.x + yellowbox.width) {
					nowy = longgun.y - playersize;
				}
				else {
					nowy = house.y - playersize;
				}
			}
			else if (x + playersize <= house.x) {
				nowy = shortgun.y - playersize;
			}
			else {
				nowy = temp;
			}
			y += jumpspeed;
			show(x, y, x2, y2);
			if (y >= nowy) {
				down = false;
			}
		}
		if (up || down) {
			playerspeed = 1;
		}
		else {
			playerspeed = 2;
		}
		//p1进行跳跃互不影响
		if (up2) {
			if (x2 >= house.x - playersize && x2 <= 1024) {
				if (x2 + playersize >= yellowbox.x && x2 + playersize <= yellowbox.x + yellowbox.width) {
					nowy2 = yellowbox.y - playersize;
				}
				else if (x2 + playersize > yellowbox.x + yellowbox.width) {
					nowy2 = longgun.y - playersize;
				}
				else {
					nowy2 = house.y - playersize;
				}
			}
			else if (x2 + playersize < house.x) {
				nowy2 = shortgun.y - playersize;
			}
			else {
				nowy2 = temp2;
			}
			y2 -= jumpspeed;
			show(x, y, x2, y2);
			if (y2 <= temp2 - jumph) {
				up2 = false;
			}
		}
		if (down2 && !up2) {
			if (x2 > house.x - playersize && x <= 1024) {
				if (x2 + playersize >= yellowbox.x && x2 + playersize <= yellowbox.x + yellowbox.width) {
					nowy2 = yellowbox.y - playersize;
				}
				else if (x2 + playersize > yellowbox.x + yellowbox.width) {
					nowy2 = longgun.y - playersize;
				}
				else {
					nowy2 = house.y - playersize;
				}
			}
			else if (x2 + playersize <= house.x) {
				nowy2 = shortgun.y - playersize;
			}
			else {
				nowy2 = temp2;
			}
			y2 += jumpspeed;
			show(x, y, x2, y2);
			if (y2 >= nowy2) {
				down2 = false;
			}
		}
		if (up2 || down2) {
			playerspeed2 = 1;
		}
		else {
			playerspeed2 = 2;
		}
		//p2进行跳跃互不影响
		newMPtime = clock();
		//回复蓝条
		if (newMPtime - startMPtime >= 2000) {
			if (currentMP <= 100) {
				currentMP += timenewMP;
			}
			if (currentMP >= 100) {
				currentMP = 100;
			}
			startMPtime = newMPtime;
		}
		newMPtime2 = clock();
		//回复蓝条
		if (newMPtime2 - startMPtime2 >= 2000) {
			if (currentMP2 <= 100) {
				currentMP2 += timenewMP;
			}
			if (currentMP2 >= 100) {
				currentMP2 = 100;
			}
			startMPtime2 = newMPtime2;
		}
		playermove(&x, &y, &x2, &y2);
		show(x, y, x2, y2);
		pdobs(&x, &y);
		pdobs(&x2, &y2);
		frameTime = clock() - frameStart;
		if (frameDelay - frameTime > 0) {
			Sleep(frameDelay - frameTime);
		}//延迟时间
		if (pdwin()) {
			break;
		}
	}
	if (currentHP == 0 && currentHP2 != 0) {
		p2win = true;
	}
	else if (currentHP2 == 0 && currentHP != 0) {
		p1win = true;
	}
	return;
}
//游戏实现逻辑
void beforestart() {
	rl = true;
	up = false;
	down = false;
	rl2 = false;
	up2 = false;
	down2 = false;
	battack = false;
	battack2 = false;
	bskill = false;
	bskill2 = false;
	p1win = false;
	p2win = false;
	visible = false;
	currentHP = 100;
	currentMP = 100;
	currentHP2 = 100;
	currentMP2 = 100;
	xHP;
	yHP;
	timeCounter = 0;
	return;
}
//初始化
void game() {
	cleardevice();
	putimage(0, 0, &start);
	beforestart();
	if (judges()) {
		selectMaps();
		setobs();
		if (gamecount == 0) {
			putimage(0, 0, &gamesm);
			gamecount++;
			Sleep(3000);
		}
		//putimage(0, 250, rplayeronem + 0, SRCAND);
		int x = longgun.x + playersize;
		int y = longgun.y - 60;
		int x2 = 1024 - playersize;
		int y2 = longgun.y - 60;
		show(x, y, x2, y2);
		playertrue(x, y, x2, y2);
		if (p1win) {
			putimage(362, 150, &endp1);
			Sleep(4000);
			game();
		}
		if (p2win) {
			putimage(362, 150, &endp2);
			Sleep(4000);
			game();
		}
		closegraph();
		exit(0);
	}
	else {
		closegraph(); // 关闭图形窗口
		exit(0); // 退出程序
	}
}
//游戏进行
int main() {
	initgraph(1024, 600, NOMINIMIZE);//创建窗口
	initimage();
	setbkmode(TRANSPARENT);//将所有要绘图的矩形背景设置为透明
	game();
}
//主函数
